<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

session_start();
session_regenerate_id();

/*
 * Start timing our time!
 * @since rev 04
 */
$timers['boot_start'] = microtime(true);

/*
 * Get our config file
 * @since rev 01
 */
require_once("config.php");

/*
 * Define Basic Constants
 * @since rev 01
 */
define("CORE_PATH", PATH."core/");
define("APPLICATION_PATH", PATH."applications/");
define("LIBRARY_PATH", CORE_PATH."libs/");
define("DESIGN_PATH", PATH."design/");
define("HOOKS_PATH", PATH."hooks/");

define("CF_VERSION", "1.0.0 Beta 1");
define("CF_REAL_VERSION", 9999);
define("CF_BUILD_VERSION", "hybrid_G4_r152 (Developer Preview)");

/*
 * Enable all error reporting
 * This is intentional - it doesn't check for IN_DEV, and its not
 * supposed to. All errors are to be reported
 * @since rev 01
 */
error_reporting(E_ALL);

/*
 * First make sure that we aren't running some evil things that we don't
 * support, and fix some as needed
 * @since rev 02
 */
require_once(CORE_PATH."terminator.php");

/*
 * Get our error handler working
 * @since rev 01
 */
require_once(CORE_PATH."errorhandler.php");

/*
 * Load our debug class
 * @since rev 04
 */
require_once(CORE_PATH."class.debug.php");
$Debug = new Debug;

/*
 * cURL Class
 * @since rev 06
 */

// Strangely, the class name 'curl' remembered me of Rip Curl. :P
// And no, I'm not a surf guy - I just see that brand too much at school.
require_once(CORE_PATH."class.curl.php");
$cURL = new curl;
$Debug->writeLog("Loaded cURL Class", "Core");

/*
 * Update System
 * @since rev 06
 */
require_once(CORE_PATH."updatemanager.php");

/*
 * Initialize Smarty.
 * @since rev 01
 */
define("SMARTY_TEMPLATES_DIR", DESIGN_PATH."templates");
define("SMARTY_COMPILED_TEMPLATES_DIR", SMARTY_TEMPLATES_DIR."/templates_c");
define("SMARTY_CONFIGS_DIR", DESIGN_PATH."configs");
require_once(LIBRARY_PATH."smarty/Smarty.class.php");
$Smarty = new smarty;
$Smarty->assign("time", date('l jS F Y h:i A'));
$Debug->writeLog("Smarty Loaded", "Core");

/*
 * Initialize our Database Class and Interface
 * @since rev 01
 */
require_once(CORE_PATH."interface.dbdriver.php");
require_once(CORE_PATH."class.database.php");
$DB = new DB;
$Debug->writeLog("Database System Loaded", "Core");

/*
 * Some language stuff...
 * @since rev 02
 */
require_once(CORE_PATH."class.language.php");
$Lang = new Language;
$Smarty->assign("Lang", $Lang);
$Debug->writeLog("Language Module Loaded", "Core");

/*
 * Maybe initialize our DB-based configs,
 * and other common tasks
 * @since rev 02
 */
require_once(CORE_PATH."common.php");
$Main = new Main;
// Make it available in smarty too
$Smarty->assign("Main", $Main);
$Debug->writeLog("Common Data Loaded", "Core");

/*
 * Go get our encryption class
 * @since rev 03
 */
require_once(CORE_PATH."class.encryption.php");
$Encryption = new Encryption;
$Smarty->assign("Encryption", $Encryption);
$Debug->writeLog("Encryptor Loaded", "Core");

/*
 * Sanitize our inputs
 * @since rev 02
 * @since rev 08 IN class is initialized inside file
 */
require_once(CORE_PATH."inputprocessor.php");

/*
 * Initialize our actual layout,
 * @since rev 01
 */
require_once(CORE_PATH."class.ui.php");
$UI = new UI(APP_TYPE);
$Smarty->assign("UI", $UI);

/*
 * Hooks System
 * @since rev 07
 */
require_once(CORE_PATH."class.hooks.php");
$Hooks = new hooks();
$Smarty->assign("Hooks", $Hooks);

/*
 * Get MVC
 */
require_once(CORE_PATH."lib.controllers.php");
require_once(CORE_PATH."lib.views.php");
require_once(CORE_PATH."lib.models.php");
$Controllers = new Controllers;
$Views = new Views;
$Models = new Models;
$Debug->writeLog("MVC System Loaded", "Core");

/*
 * reCAPTCHA Support
 * @since rev 07
 */
require_once(LIBRARY_PATH."recaptcha/captcha_wrap.php");
$CAPTCHA = new recaptcha;
$Debug->writeLog("reCAPTCHA Support Loaded", "Core");

/*
 * Pagination System
 * @since rev 09
 */
require_once(CORE_PATH."class.pagination.php");
$Pagination = new pagination;
$Debug->writeLog("Loaded Pagination Class", "Core");

/*
 * AJAX support
 */
if(isset($_GET['ajax'])) define("IN_AJAX", 1);
elseif(isset($_SERVER["X_REQUESTED_WITH"]) && $_SERVER["X_REQUESTED_WITH"] == "XMLHttpRequest") define("IN_AJAX", 1);
else define("IN_AJAX", 0);


/*
 * Dev Things
 */
if(IN_DEV) {
	if(isset($_GET["hash"])) { // Hash get
		$Hooks->run_hooks("dev_encryption_start");
		die($Encryption->Encrypt($_GET['hash']));
	}
}

/*
 * Start Output Buffering if needed
 */
$buf = ($Main->config_value("compress_output") == "1" ? true : false);
if($buf) {
	$Debug->writeLog("Output Buffering and Compressing Active, starting now...", "Core");
	ob_start("ob_gzhandler");
	$Hooks->run_hooks("output_buffer_start");
	register_shutdown_function("ob_end_flush");
}

/*
 * End Boot Time
 * @since rev 04
 */
$timers['boot_end'] = microtime(true);
